// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   apply_check_ok.java

package draft.manufacture;

import include.nseer_cookie.counter;
import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class apply_check_ok extends HttpServlet
{

	public apply_check_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		counter count = new counter(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		try
		{
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String apply_ID = request.getParameter("apply_ID");
				String product_IDn[] = request.getParameterValues("product_ID");
				String amount1[] = request.getParameterValues("amount");
				String product_amount = request.getParameter("product_amount");
				int num = Integer.parseInt(product_amount);
				int m = 0;
				int n = 0;
				for (int j = 1; j < product_IDn.length; j++)
				{
					String sql3 = (new StringBuilder("select * from manufacture_apply where apply_ID='")).append(apply_ID).append("' and product_ID='").append(product_IDn[j]).append("'").toString();
					ResultSet rs3 = manufacture_db.executeQuery(sql3);
					if (rs3.next())
						m++;
				}

				if (num == 0 && product_IDn.length == 1)
					response.sendRedirect((new StringBuilder("draft/manufacture/apply_ok_a.jsp?apply_ID=")).append(apply_ID).toString());
				else
				if (m != 0)
				{
					response.sendRedirect((new StringBuilder("draft/manufacture/apply_ok_b.jsp?apply_ID=")).append(apply_ID).toString());
				} else
				{
					for (int i = 1; i <= num; i++)
					{
						String amount_i = request.getParameter((new StringBuilder("amount")).append(i).toString());
						if (amount_i.equals("") || !validata.validata(amount_i))
							n++;
					}

					for (int j = 1; j < product_IDn.length; j++)
						if (!validata.validata(amount1[j]))
							n++;

					if (n == 0)
					{
						String register = request.getParameter("register");
						String register_time = request.getParameter("register_time");
						String bodyb = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
						String remark = exchange.toHtml(bodyb);
						try
						{
							String sql1 = (new StringBuilder("delete from manufacture_apply where apply_ID='")).append(apply_ID).append("' and product_ID=''").toString();
							manufacture_db.executeUpdate(sql1);
							for (int i = 1; i <= num; i++)
							{
								String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
								String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
								String tem_product_describe = (new StringBuilder("product_describe")).append(i).toString();
								String tem_amount = (new StringBuilder("amount")).append(i).toString();
								String product_name = request.getParameter(tem_product_name);
								String product_ID = request.getParameter(tem_product_ID);
								String product_describe = request.getParameter(tem_product_describe);
								String amount = request.getParameter(tem_amount);
								sql1 = (new StringBuilder("update manufacture_apply set product_ID='")).append(product_ID).append("',product_name='").append(product_name).append("',product_describe='").append(product_describe).append("',amount='").append(amount).append("',remark='").append(remark).append("',register='").append(register).append("',register_time='").append(register_time).append("' where apply_ID='").append(apply_ID).append("' and product_ID='").append(product_ID).append("'").toString();
								manufacture_db.executeUpdate(sql1);
							}

							String product_namen[] = request.getParameterValues("product_name");
							String product_describen[] = request.getParameterValues("product_describe");
							String amountn[] = request.getParameterValues("amount");
							String type[] = request.getParameterValues("type");
							for (int j = 1; j < product_IDn.length; j++)
							{
								String sql4 = (new StringBuilder("insert into manufacture_apply(apply_ID,product_ID,product_name,product_describe,amount,remark,type,register,register_time) values('")).append(apply_ID).append("','").append(product_IDn[j]).append("','").append(product_namen[j]).append("','").append(product_describen[j]).append("','").append(amountn[j]).append("','").append(remark).append("','").append(type[j]).append("','").append(register).append("','").append(register_time).append("')").toString();
								manufacture_db.executeUpdate(sql4);
							}

							List rsList = new ArrayList();
							String elem[] = new String[3];
							String sql = "select id,describe1,describe2 from manufacture_config_workflow where type_id='03'";
							for (ResultSet rset = manufacture_db.executeQuery(sql); rset.next(); rsList.add(elem))
							{
								elem = new String[3];
								elem[0] = rset.getString("id");
								elem[1] = rset.getString("describe1");
								elem[2] = rset.getString("describe2");
							}

							if (rsList.size() == 0)
							{
								String pay_ID_group = "";
								sql = (new StringBuilder("update manufacture_apply set check_tag='1' where apply_ID='")).append(apply_ID).append("'").toString();
								manufacture_db.executeUpdate(sql);
								sql = (new StringBuilder("select * from manufacture_apply where apply_ID='")).append(apply_ID).append("'").toString();
								ResultSet rs1 = manufacture_db.executeQuery(sql);
								if (rs1.next())
									pay_ID_group = rs1.getString("pay_ID_group");
								for (StringTokenizer tokenTO = new StringTokenizer(pay_ID_group, ", "); tokenTO.hasMoreTokens();)
								{
									sql = (new StringBuilder("select * from stock_pay where pay_ID='")).append(tokenTO.nextToken()).append("'").toString();
									rs1 = manufacture_db.executeQuery(sql);
									if (rs1.next())
									{
										sql = (new StringBuilder("update crm_order set manufacture_tag='2' where order_ID='")).append(rs1.getString("reasonexact")).append("'").toString();
										manufacture_db.executeUpdate(sql);
									}
								}

							} else
							{
								sql = (new StringBuilder("delete from manufacture_workflow where object_ID='")).append(apply_ID).append("'").toString();
								manufacture_db.executeUpdate(sql);
								sql = (new StringBuilder("update manufacture_apply set check_tag='0' where apply_ID='")).append(apply_ID).append("'").toString();
								manufacture_db.executeUpdate(sql);
								for (Iterator ite = rsList.iterator(); ite.hasNext(); manufacture_db.executeUpdate(sql))
								{
									elem = (String[])ite.next();
									sql = (new StringBuilder("insert into manufacture_workflow(config_id,object_ID,describe1,describe2,type_id) values ('")).append(elem[0]).append("','").append(apply_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("','03')").toString();
								}

							}
						}
						catch (Exception ex)
						{
							ex.printStackTrace();
						}
						response.sendRedirect("draft/manufacture/apply_ok.jsp?finished_tag=4");
					} else
					{
						response.sendRedirect((new StringBuilder("draft/manufacture/apply_ok_c.jsp?apply_ID=")).append(apply_ID).toString());
					}
				}
				manufacture_db.commit();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
